(function($) {
	$(document).ready(function() {
		
		
		/*
		$("#addLeagueButton").livequery('click',function() {
			var action = 'LeagueEditor_Controller/addLeague';
			var name = $("#newLeagueName").val();
			var id = $("#Form_EditForm_ID").val();

			//send ajax request to the page
			$.ajax({
				type: "POST",
				url: action,
				data: 'name='+ name + '&id=' + id,
				
				// create a new field
				success: function(response){
					if (response != '')
						$('#leagueTables').html(response);
				},
				
				// error creating new field
				error: function(request, text, error) {
					alert('error saving data');
				} 
			});			
			
			return false;
		});
		
		
		
		$('input[name*=addTeam]').livequery('click',function() {
			var attr = this.name.split('-');
			var leagueId = parseInt(attr[1]);
			var teamId = parseInt($(this).prev('select').val());
			
			if (leagueId && teamId) {
				var action = 'LeagueEditor_Controller/addTeamToLeague';
				$.ajax({
					type: "POST",
					url: action,
					data: 'leagueId='+ leagueId + '&teamId=' + teamId,
					
					// create a new field
					success: function(response){
						if (response != '')
							$('#leagueTables').html(response);
						//$("CwebDebug").html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});					
			}

			
			return false;
		});
		
		*/
		$('input[name*=editGame]').livequery('click',function() {
			var currentRow = $(this).parent().parent();
			currentRow.css('display','none');
			currentRow.next('tr').css('display','table-row');
			return false;
		});
		
		$('input[name*=saveGame]').livequery('click',function() {
			var currentRow = $(this).parent().parent();
			currentRow.css('display','none');
			var targetRow = currentRow.prev('tr');
			targetRow.css('display','table-row');
			var params = new Object();
			$(this).parent().parent().find('input,select').each( function (i,el) {
					var name =  el.name.split('-')[0];
					if (name == 'saveGame') {
						params['gameId'] = el.name.split('-')[1];
					} else {
						if (el.type == 'checkbox' && !el.checked) {

						} else {
							params[name] = $(el).val();
						}
					}
			});
			var postData = '';
			for (param in params) {
				postData += param + "=" + params[param] + "&";
			}
			
			var action = 'GameEditor_Controller/saveGame';
			var currentTable = $(this).parents('div')[0];
			$.ajax({
				type: "POST",
				url: action,
				data: postData,
				
				// create a new field
				success: function(response){
					if (response != '') 
						//$('#GameTables').html(response);
					//$("CwebDebug").html(response);
						$(currentTable).html(response);
				},
				
				// error creating new field
				error: function(request, text, error) {
					alert('error saving data');
				} 
			});	
			
			return false;
		});
		
		$('img[class=showRound]').livequery('click',function() {
			var attr = this.alt.split('-');
			var leagueId = parseInt(attr[1]);
			var round = parseInt(attr[2]);
			if (leagueId && round) {
				var currentTable = $(this).parent().parent();
				
				var action = 'GameEditor_Controller/showRound';
				
				$.ajax({
					type: "POST",
					url: action,
					data: 'leagueId='+ leagueId + '&round=' + round,
					
					// create a new field
					success: function(response){
						if (response != '')
							//$('#GameTables').html(response);
						//$("CwebDebug").html(response);
							$(currentTable).html(response);
					},
					
					// error creating new field
					error: function(request, text, error) {
						alert('error saving data');
					} 
				});			
						
			}	
			return false;
		});
		
	});
})
(jQuery);

